
  const cloud = require('wx-server-sdk')
  cloud.init({
    env: "test4-ht5oe"
  })

  const xlsx = require('node-xlsx');
// 云函数入口函数
exports.main = async (event, context) => {
        try {
          let userdata = event.datalist
          let length_two =  event.datalist.length
          console.log("2222"+length_two)
          //1,定义excel表格名
          let dataCVS = '员工业绩汇总表.xlsx'
          //2，定义存储数据的
          let alldata = [];
          let row = ['员工编号', '员工姓名','员工级别', '员工电话','出售时间','商品编号','商品名称','商品单价','销售数量','服务用户']; //表属性
          alldata.push(row);
        // console.log("哈哈",userdata)
        // console.log("哈哈",userdata[0])
        for(let i=0;i<length_two;i++){
          for (let key in userdata[i]) {
            let _userdata = userdata[i]
            let arr = [];
            console.log("1111"+key)
            console.log("1111"+_userdata[key])
            arr.push(_userdata[key].TechID);
            arr.push(_userdata[key].TechName);
            arr.push(_userdata[key].TechGrade);
            arr.push(_userdata[key].teachID);
            arr.push(_userdata[key].date);
            arr.push(_userdata[key].goodsID);
            arr.push(_userdata[key].goodsName);
            arr.push(_userdata[key].unitPrice);
            arr.push(_userdata[key].salenum);
            arr.push(_userdata[key].customerID);
            console.log("哈哈哈哈"+arr)
            alldata.push(arr)
          }
        }
          //3，把数据保存到excel里
          var buffer = await xlsx.build([{
            name: "mySheetName",
            data: alldata
          }]);
          //4，把excel文件保存到云存储里
          return await cloud.uploadFile({
            cloudPath: dataCVS,
            fileContent: buffer, //excel二进制文件
          })
        
        } catch (e) {
          console.error(e)
          return e
        }

 

}

